package main

import (
	"net/http"
	"apis"
	"config"
	"loop"
	"fmt"
	"libs/graceful"
	"libs"
	"path"
	"log"
)

func main() {
	if config.Debug {
		log.SetFlags(log.Lshortfile | log.LstdFlags)
	} else {
		libs.SetLogFile(path.Join(path.Join(config.WorkDir, "access.log")))
	}
	// 监听key过期事件
	go loop.Subscribe(fmt.Sprintf("__keyevent@%s__:expired", config.RedisDatabase))
	// 监测topic的堆积情况
	go loop.Monitor()
	// 路由
	http.HandleFunc("/tigris/ping", ping)
	http.HandleFunc("/message/post/", apis.Producer)
	http.HandleFunc("/message/get/", apis.Consumer)
	http.HandleFunc("/message/delete/", apis.Callback)

	graceful.ListenAndServe(&http.Server{Addr: config.Listener}, path.Join(config.WorkDir, "tigris.pid"))
}

func ping(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte{111, 107})
}
